package com.agriculture.controller;

import com.agriculture.entity.GeoDevice;
import com.agriculture.service.GeoDeviceService;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
 * GeoDeviceController
 * @description: GeoDeviceController
 * &#064;author:  wms
 */
@RestController
@RequestMapping("/devices")
@CrossOrigin(origins = "*")
public class GeoDeviceController {
    
    @Autowired
    private GeoDeviceService deviceService;
    
    private final GeometryFactory geometryFactory = new GeometryFactory();
    
    @GetMapping
    public List<GeoDevice> getAllDevices() {
        return deviceService.getAllDevices();
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<GeoDevice> getDeviceById(@PathVariable Integer id) {
        Optional<GeoDevice> device = deviceService.getDeviceById(id);
        if (device.isPresent()) {
            return ResponseEntity.ok(device.get());
        } else {
            throw new EntityNotFoundException("设备不存在");
        }
    }
    
    @PostMapping
    public ResponseEntity<GeoDevice> createDevice(@RequestBody Map<String, Object> deviceData) {
        GeoDevice device = new GeoDevice();
        device.setDeviceName((String) deviceData.get("deviceName"));
        device.setDeviceType((String) deviceData.get("deviceType"));
        device.setStatus(GeoDevice.DeviceStatus.valueOf((String) deviceData.get("status")));
        
        // 处理位置数据
        Map<String, Double> position = (Map<String, Double>) deviceData.get("currentPosition");
        if (position != null) {
            Point point = geometryFactory.createPoint(
                new Coordinate(position.get("x"), position.get("y"))
            );
            device.setCurrentPosition(point);
        }
        
        GeoDevice savedDevice = deviceService.saveDevice(device);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedDevice);
    }
    
    @PutMapping("/{id}/position")
    public ResponseEntity<GeoDevice> updateDevicePosition(@PathVariable Integer id, 
                                                         @RequestParam Double longitude, 
                                                         @RequestParam Double latitude) {
        GeoDevice device = deviceService.updateDevicePosition(id, longitude, latitude);
        if (device != null) {
            return ResponseEntity.ok(device);
        } else {
            throw new EntityNotFoundException("设备不存在");
        }
    }
    
    @GetMapping("/status/{status}")
    public List<GeoDevice> getDevicesByStatus(@PathVariable String status) {
        try {
            GeoDevice.DeviceStatus deviceStatus = GeoDevice.DeviceStatus.valueOf(status.toUpperCase());
            return deviceService.getDevicesByStatus(deviceStatus);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("无效的设备状态: " + status);
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDevice(@PathVariable Integer id) {
        deviceService.deleteDevice(id);
        return ResponseEntity.ok().build();
    }
}